package com.chainyoung.component_tmperson.ui.mvp.presenter;

import android.util.Log;

import com.chainyoung.common.appcommon.entity.HdWalletCache;
import com.chainyoung.common.base.CommonSubscriber;
import com.chainyoung.common.exception.ApiException;
import com.chainyoung.common.mvp.BasePresenter;
import com.chainyoung.common.utils.RxUtils;
import com.chainyoung.component_tmperson.entity.BookMark;
import com.chainyoung.component_tmperson.entity.BrowseCache;
import com.chainyoung.component_tmperson.entity.CheckBookMarkResult;
import com.chainyoung.component_tmperson.ui.mvp.contract.HdWalletSendTransactionContract;
import com.chainyoung.component_tmperson.ui.mvp.contract.OtheAppSendTransactionContract;

import org.json.JSONException;
import org.web3j.crypto.CipherException;

import java.io.IOException;
import java.util.List;
import java.util.TreeMap;

import javax.inject.Inject;

import static com.chainyoung.common.constant.LibConstant.NO_RESULT_ERRORCODE;

public class OtherAppSendTransactionPresenter extends BasePresenter<OtheAppSendTransactionContract.Model, OtheAppSendTransactionContract.View> {


    @Inject
    public OtherAppSendTransactionPresenter(OtheAppSendTransactionContract.Model model, OtheAppSendTransactionContract.View rootView) {
        super(model, rootView);
    }


    public void getGasPrice() {
        addSubscribe(
                mModel.getGasPrice()
                        .compose(RxUtils.<String>rxSchedulerHelper())
                        .subscribeWith(new CommonSubscriber<String>(mView) {
                            @Override
                            protected void onStart() {
                                super.onStart();
                                mView.showLoading("加载中");
                            }

                            @Override
                            public void onComplete() {
                                mView.hideLoading();
                            }

                            @Override
                            public void onNext(String data) {
                                mView.onGetGasPriceSuccess(data);
                            }
                        })
        );
    }


    public void sendTransaction(String hex) {
        addSubscribe(
                mModel.sendTransaction(hex)
                        .compose(RxUtils.<String>rxSchedulerHelper())
                        .subscribeWith(new CommonSubscriber<String>(mView) {
                            @Override
                            protected void onStart() {
                                super.onStart();
                                mView.showLoading("交易处理中");
                            }

                            @Override
                            public void onComplete() {
                                mView.hideLoading();
                            }

                            @Override
                            public void onNext(String data) {
                                mView.onSendTransactionSuccess(data);
                            }

                            @Override
                            public void onError(Throwable e) {
                                super.onError(e);
                                mView.onSendTransactionFailed();
                            }
                        })
        );
    }


    public void getNonce(String addr) {
        addSubscribe(
                mModel.getNonce(addr)
                        .compose(RxUtils.<String>rxSchedulerHelper())
                        .subscribeWith(new CommonSubscriber<String>(mView) {
                            @Override
                            protected void onStart() {
                                super.onStart();
                                mView.showLoading("交易处理中");
                            }

                            @Override
                            public void onComplete() {
                                mView.hideLoading();
                            }

                            @Override
                            public void onNext(String data) {
                                try {
                                    mView.onGetNonce(data);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                } catch (CipherException e) {
                                    e.printStackTrace();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        })
        );
    }

    public void checkIsUrlCollected(TreeMap<String, Object> map) {
        addSubscribe(mModel.checkIsUrlCollected(map)
                .compose(RxUtils.<CheckBookMarkResult>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<CheckBookMarkResult>(mView) {
                    @Override
                    public void onNext(CheckBookMarkResult data) {
                        mView.onCheckIsUrlCollected(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                }));
    }

    public void delBookmark(TreeMap<String, Object> map) {
        addSubscribe(mModel.delBookmark(map)
                .compose(RxUtils.<List<BookMark>>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<List<BookMark>>(mView) {
                    @Override
                    public void onNext(List<BookMark> data) {
                        mView.onDelBookmarkSuccess(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof ApiException) {
                            if (((ApiException) e).getErrorCode().equals(NO_RESULT_ERRORCODE)) {
                                mView.onDelBookmarkSuccess(null);
                                return;
                            }
                        }
                        super.onError(e);
                    }
                }));
    }


    public void addBookmark(TreeMap<String, Object> map) {
        addSubscribe(mModel.addBookmark(map)
                .compose(RxUtils.<List<BookMark>>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<List<BookMark>>(mView) {
                    @Override
                    public void onNext(List<BookMark> data) {
                        mView.onAddBookmarkSuccess(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof ApiException) {
                            if (((ApiException) e).getErrorCode().equals(NO_RESULT_ERRORCODE)) {
                                mView.onAddBookmarkSuccess(null);
                                return;
                            }
                        }
                        super.onError(e);
                    }
                }));
    }

    public void addBrowseCache(BrowseCache item) {
        addSubscribe(mModel.addBrowseCache(item)
                .compose(RxUtils.<BrowseCache>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<BrowseCache>(mView) {
                    @Override
                    public void onNext(BrowseCache data) {
                        //mView.onAddBookmarkSuccess(data);
                        Log.d("BrowseCache", "success");
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                }));

    }


    public void getAllHdWalletCaches(String coinType) {
        addSubscribe(mModel.getAllHdWalletCaches(coinType)
                .compose(RxUtils.<List<HdWalletCache>>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<List<HdWalletCache>>(mView) {
                    @Override
                    public void onNext(List<HdWalletCache> data) {
                        mView.onGetAllHdWalletCaches(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                }));


    }


    public void addHdWalletCache(HdWalletCache item) {
        addSubscribe(mModel.addHdWalletCache(item)
                .compose(RxUtils.<HdWalletCache>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<HdWalletCache>(mView, true) {
                    @Override
                    public void onNext(HdWalletCache data) {
                        mView.onAddHdWalletCacheSuccess(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                }));
    }


    public void getSingleEthBalance(final String addr) {
        addSubscribe(
                mModel.getSingleEthBalance(addr)
                        .compose(RxUtils.<String>rxSchedulerHelper())
                        .subscribeWith(new CommonSubscriber<String>(mView) {

                            @Override
                            public void onNext(String data) {
                                mView.onGetSingleEthBalanceSuccess(data, addr);
                            }

                            @Override
                            public void onError(Throwable e) {
                                super.onError(e);
                            }
                        })
        );
    }


}


